'use strict'
var express = require('express'),
    router = express.Router(),
    Product = require('../models/product.js'),
    ProPic = require('../models/proPic.js'),
    fs = require('fs'),
    path = require('path'),
    formidable = require('formidable'),
    moment = require('moment'),
    uuid = require('node-uuid'),
    Page = require('../models/page.js');

var db = require('../models/db');

function checkRootLogin(req, res, next) {
    if (!req.session.employee) {
        return res.send({ "error": 400, "message": "未登录！" });
    }
    next();
}

router.get("/queryProduct", function(req, res) {
    var product = new Product({
        proName: req.query.proName ? req.query.proName : '',
        // price: req.query.price ? req.query.price : '',
        // num: req.query.num ? req.query.num : '',
        // brandId: req.query.brandId ? req.query.brandId : ''
    })
    var page = new Page({
        page: req.query.page ? parseInt(req.query.page) : 0,
        size: req.query.pageSize ? parseInt(req.query.pageSize) : 10,
    })

    db.query("SELECT * FROM product", function(err, data) {
        if (err) {
            return callback(err);
        }
        //      console.log("queryProduct");
        //   console.log(data);

        Product.countProduct(function(err, result) {
            if (err) return res.send({ "error": 403, "message": "数据库异常！" });
            if (data.length == 0) {
                page.count = result.count;
                page.data = data;
                return res.send(page);
            }
            var idStr = "";
            for (let i = 0; i < data.length; i++) {
                if (i == 0) {
                    idStr += data[i].id
                } else {
                    idStr += "," + data[i].id
                }

                data[i].pic = new Array();
            }
            //   console.log("idStr" + idStr);
            ProPic.queryPic(idStr, function(err, picData) {
                //  console.log(picData);
                if (err) return res.send({ "error": 403, "message": "数据库异常！" });
                for (let l = 0; l < picData.length; l++) {
                    for (let n = 0; n < data.length; n++)
                        if (data[n].id == picData[l].productId) {
                            data[n].pic[data[n].pic.length] = picData[l];
                        }
                }
                page.count = result.count;
                page.data = data;
                res.send(page);
            })

        })
    });
});

router.get("/queryProductDetail", function(req, res) {
    Product.queryProductDetail(req.query.id, function(err, data) {
        if (err) return res.send({ "error": 403, "message": "数据库异常！" });
        data.pic = new Array();
        ProPic.queryPic(req.query.id, function(err, picData) {
            if (err) return res.send({ "error": 403, "message": "数据库异常！" });
            if (picData && picData.length) {
                data.pic = picData;
            }
            res.send(data);
        })
    })
});

router.post("/addProductPic", function(req, res) {
    console.log('===============');
    //创建表单上传
    var form = new formidable.IncomingForm();
    //设置编辑
    form.encoding = 'utf-8';
    //设置文件存储路径
    form.uploadDir = "public/upload/product";
    //保留后缀
    form.keepExtensions = true;
    //设置单文件大小限制 2m
    form.maxFieldsSize = 2 * 1024 * 1024;
    //form.maxFields = 1000;  设置所以文件的大小总和
    form.parse(req, function(err, fields, files) {
        for (let i = 1; i < 4; i++) {
            var file = files['pic' + i];
            if (!file || file.name == "") break;
            let picName = uuid.v1() + path.extname(file.name);
            fs.rename(file.path, 'public\\upload\\product\\' + picName, function(err) {
                if (err) res.send({ "error": 403, "message": "图片保存异常！" });
                res.send({ "picName": picName, "picAddr": '/upload/product/' + picName });

            })
        }
    });
});
router.post("/addProduct", checkRootLogin);

var addPic = function(picName, picAddr, id) {
    ProPic.addPic({
        picName: picName,
        productId: id,
        picAddr: picAddr
    }, function(err, data) {
        console.log("加入一张图片成功！");
    })
}
router.post("/addProduct", function(req, res) {

    var product = new Product({
        proName: req.body.proName ? req.body.proName : '',
        oldPrice: req.body.oldPrice ? parseFloat(req.body.oldPrice) : '',
        price: req.body.price ? parseFloat(req.body.price) : '',
        proDesc: req.body.proDesc ? req.body.proDesc : '',
        size: req.body.size ? req.body.size : '',
        statu: req.body.statu ? parseInt(req.body.statu) : '1',
        updateTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        num: req.body.num ? parseInt(req.body.num) : '',
        brandId: req.body.brandId ? parseInt(req.body.brandId) : ''
    })
    Product.addProduct(product, function(err, data) {
        if (err) return res.send({ "error": 403, "message": "数据库异常！" });

        if (req.body.picName1 && req.body.picAddr1) {
            addPic(req.body.picName1, req.body.picAddr1, data.insertId);
        }
        if (req.body.picName2 && req.body.picAddr2) {
            addPic(req.body.picName2, req.body.picAddr2, data.insertId);
        }
        if (req.body.picName3 && req.body.picAddr3) {
            addPic(req.body.picName3, req.body.picAddr3, data.insertId);
        }
        res.send({ "success": true });
    })

});
router.post("/updateProduct", checkRootLogin);
router.post("/updateProduct", function(req, res) {
    var product = new Product({
        id: req.body.id ? parseInt(req.body.id) : '',
        proName: req.body.proName ? req.body.proName : '',
        oldPrice: req.body.oldPrice ? parseFloat(req.body.oldPrice) : '',
        price: req.body.price ? parseFloat(req.body.price) : '',
        proDesc: req.body.proDesc ? req.body.proDesc : '',
        size: req.body.size ? req.body.size : '',
        statu: req.body.statu ? parseInt(req.body.statu) : '',
        updateTime: moment().format("YYYY-MM-DD HH:mm:ss"),
        num: req.body.num ? parseInt(req.body.num) : '',
        brandId: req.body.brandId ? parseInt(req.body.brandId) : ''
    })
    Product.updateProduct(product, function(err, data) {
        if (err) return res.send({ "error": 403, "message": "数据库异常！" });

        ProPic.delPic(req.body.id, function(err, picData) {
            if (err) return res.send({ "error": 403, "message": "数据库异常！" });
            if (req.body.picName1 && req.body.picAddr1) {
                addPic(req.body.picName1, req.body.picAddr1, req.body.id);
            }
            if (req.body.picName2 && req.body.picAddr2) {
                addPic(req.body.picName2, req.body.picAddr2, req.body.id);
            }
            if (req.body.picName3 && req.body.picAddr3) {
                addPic(req.body.picName3, req.body.picAddr3, req.body.id);
            }
        })


        res.send({ "success": true });
    })
});

router.get("/queryProductDetail", function(req, res) {
    Product.queryProductDetail(req.query.id, function(err, data) {
        if (err) return res.send({ "error": 403, "message": "数据库异常！" });
        data.pic = new Array();
        ProPic.queryPic(req.query.id, function(err, picData) {
            if (err) return res.send({ "error": 403, "message": "数据库异常！" });
            if (picData && picData.length) {
                data.pic = picData;
            }
            res.send(data);
        })
    })
});
module.exports = router;